<template>
    <views class="terrace_role_add">
        <terrace-role-detail 
        :type="type" 
        :permission_ids="permission_ids" 
        :default_expanded_keys="default_expanded_keys"  
        @submitRole="submitRole">
        </terrace-role-detail>

    </views>
</template>





<script>
    import TerraceRoleDetail from 'components/terrace/terrace_role/child_component/chile_component/terrace_role_detail';


export default {
    name: 'TerraceRoleAdd',
    data(){
        return  {
            type:1,
            permission_ids:[],
            default_expanded_keys:[],        //默认展开tree 的 id
        }
    },
    components:{
        TerraceRoleDetail
    },
    mounted(){
        this.$setRouteTitle();
        //新增时，只需要 获取平台选权限树 即可
        this.$raxios.post(this.$mapapi.getAdminPermission,{}).then(res => {
            if(res.code != 200)    return Promise.reject(res.msg);
            let r_data = res.data;
            if(r_data.length == 0)   return Promise.reject('权限树为空');
            //新增的时候，将权限树全部展开
            for(let val of r_data){
                this.permission_ids.push(val);
                this.default_expanded_keys.push(val.permission_id);
                if(val.list.length > 0){
                    for(let vo of val.list){
                        this.default_expanded_keys.push(vo.permission_id);
                        if(vo.list.length > 0){
                            for(let vi of vo.list){
                                this.default_expanded_keys.push(vi.permission_id);
                            }
                        }
                    }
                }
            }
        }).catch(res => {
            this.$message.error(res);
        });
    },
    methods:{
        submitRole(data){
            console.log(data);
            this.$raxios.post(this.$mapapi.saveAdminRole,{data}).then(res =>{
                if(res.code != 200)     return  Promise.reject(res.msg);
                this.$message.success(res.msg);
                setTimeout(
                    this.$router.push('/terrace_role')
                    ,2000);
            }).catch(res => {
                console.log(res);
                this.$message.error(res);
            });

        }
    }
}
</script>

<style :scope="true">
    .terrace_role_add{
        margin: 10px
    }
</style>

